﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;

namespace Landscape
{
    sealed class Globals
    {
        public static Configuration Config = System.Configuration.ConfigurationManager.GetSection("landscape") as Configuration;

        private Globals() { }

        #region Path management

        public static string AbsPath(string path)
        {
            return System.Web.HttpContext.Current.Server.MapPath("/") + path;
        }

        // tries to find a find into specified sources and returns its path or null
        // stops at first match
        public static string LookupFile(string filename, List<KeyValuePair<int, string>> sourcePaths) {
            if (sourcePaths != null && sourcePaths.Count > 0) {
                foreach (KeyValuePair<int, string> sourceDir in sourcePaths)
                {
                    string currentPath = sourceDir.Value + "/" + filename;
                    if (System.IO.File.Exists(currentPath))
                    {
                        return currentPath;
                    }
                }                
            }

            return null;
        }
        #endregion

        #region Strings
        public static string UCFirst(string s)
        {
            // Check for empty string.
            if (string.IsNullOrEmpty(s))
            {
                return string.Empty;
            }
            // Return char and concat substring.
            return char.ToUpper(s[0]) + s.Substring(1);
        }
        #endregion

        #region Streams
        public static MemoryStream GetStreamFromString(string s) {
            MemoryStream streamResult = new MemoryStream();
            StreamWriter streamResultWriter = new StreamWriter(streamResult);
            streamResultWriter.Write(s);
            streamResultWriter.Flush();

            streamResult.Position = 0;

            return streamResult;
        }
        #endregion

        #region Page Extension
        public static System.Web.UI.Control FindControlRecursive(System.Web.UI.Control ctrl, string controlID) { 
            if (string.Compare(ctrl.ID, controlID, true) == 0) { 
                // We found the control! 
                return ctrl; 
            } else { 
                // Recurse through ctrl's Controls collections 
                foreach (System.Web.UI.Control child in ctrl.Controls) {
                    System.Web.UI.Control lookFor = FindControlRecursive(child, controlID); 

                    if (lookFor != null) return lookFor; // We found the control 
                } 
                // If we reach here, control was not found 
                return null; 
            }
        }
        #endregion

        #region Reflection methods
        public static Dictionary<string, string> GetClassProperties(System.Type t, string[] allowedTypes)
        {
            PropertyInfo[] props = t.GetProperties(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public);
            Dictionary<string, string> validProps = new Dictionary<string, string>();
            if(props.Length > 0){
                foreach(PropertyInfo p in props){
                    string pType = p.PropertyType.FullName;
                    if (Array.IndexOf(allowedTypes, pType) >= 0)
                    {
                        validProps.Add(p.Name, pType);
                    }
                }
            }

            return validProps;
        }
        #endregion
    }
}
